﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apps.IBLL;
using Microsoft.Practices.Unity;
using Apps.IDAL;
using Apps.Models;
using Apps.BLL.Core;
using Apps.Common;
using System.Transactions;
using Apps.Models.Sys;
using Apps.Locale;
using Apps.IDAL.Sys;

namespace Apps.BLL.Sys
{
    public partial class SysModuleBLL
    {

        [Dependency]
        public ISysModuleOperateRepository m_OperateRep { get; set; }


        public List<SysModuleModel> GetList(string parentId, bool all = true)
        {
            IQueryable<SysModule> queryData = null;
            if (all)
            {
                queryData = m_Rep.GetList(a => a.ParentId == parentId).OrderBy(a => a.Sort);
            }
            else
            {
                queryData = m_Rep.GetList(a => a.ParentId == parentId && a.Enable).OrderBy(a => a.Sort);
            }
            return CreateModelList(ref queryData);
        }

        public List<SysModuleModel> GetList(string parentId,string menuname)
        {
            IQueryable<SysModule> queryData = null;
            if (string.IsNullOrEmpty(menuname) || parentId == "0")
            {
                queryData = m_Rep.GetList(a => a.ParentId == parentId ).OrderBy(a => a.Sort);
            }
            else
            {
                string path = string.Format(",{0},", parentId);
                queryData = m_Rep.GetList(a => a.Name.Contains(menuname)).OrderBy(a => a.Sort);
                //queryData = m_Rep.GetList(a => a.PATH.Contains(path) && a.Name.Contains(menuname)).OrderBy(a => a.Sort);
            }
            return CreateModelList(ref queryData);
        }

        public List<SysModuleModel> GetListByName(string parentId, string menuname, bool isVisible = true)
        {
            IQueryable<SysModule> queryData = null;
            menuname = string.IsNullOrEmpty(menuname) ? "" : menuname;
            if (parentId == "0")
            {
                //queryData = m_Rep.GetList(a => a.ParentId == parentId && a.Name.Contains(menuname) && (isVisible ? 1 == 1 : a.Enable == true)).OrderBy(a => a.Sort);
                queryData = m_Rep.GetList(a => a.ParentId == parentId && a.Name.Contains(menuname) && (isVisible ? 1 == 1 : a.Enable == true)).OrderBy(a => a.Sort);
            }
            else
            {
                queryData = m_Rep.GetList(a => a.ParentId == parentId && (isVisible ? 1 == 1 : a.Enable == true)).OrderBy(a => a.Sort);
            }
            return CreateModelList(ref queryData);
        }

        public List<SysModuleModel> GetModuleByCreate(string parentId, string Id = "")
        {
            var ModuleList = new List<SysModuleModel>();
            var queryData2 = m_Rep.Context.SysModule.Where(m => m.ParentId == parentId && !m.IsLast && m.Id != Id).OrderBy(m => m.Sort).ToList();
            foreach (var FModule in queryData2)
            {
                var mModel = new SysModuleModel();
                mModel.Name = "--" + FModule.Name;
                mModel.Id = FModule.Id;
                ModuleList.Add(mModel);
                ModuleList.AddRange(GetChildModule(FModule, 2, Id));
            }
            return ModuleList;

        }


        public List<SysModuleModel> GetChildModule(SysModule FSysModule, int Lev, string Id = "")
        {
            string FName = "";
            FName = FName.PadLeft(Lev * 2, '-');
            var ModuleList = new List<SysModuleModel>();
            foreach (var FModule in FSysModule.SysModule1.Where(m=> !m.IsLast && m.Id != Id).OrderBy(m=>m.Sort))
            {
                var mModel = new SysModuleModel();
                mModel.Id = FModule.Id;
                mModel.Name = FName + FModule.Name;
                ModuleList.Add(mModel);
                if (FModule.SysModule1.Count > 0)
                {
                    ModuleList.AddRange(GetChildModule(FModule, Lev++));
                }
            }
            return ModuleList;
        }

        public List<SysModule> GetModuleBySystem(string parentId)
        {

            return m_Rep.GetModuleBySystem(parentId).ToList();
        }

        public override bool Create(ref ValidationErrors errors, SysModuleModel model)
        {
            try
            {
                SysModule entity = m_Rep.GetById(model.Id);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new SysModule();
                entity.Id = model.Id;
                entity.Name = model.Name;
                entity.EnglishName = model.EnglishName;
                entity.ParentId = model.ParentId;

                entity.PATH = model.PATH;
                entity.Url = model.Url;
                entity.Iconic = model.Iconic;
                entity.Sort = model.Sort;
                entity.Remark = model.Remark;
                entity.Enable = model.Enable;
                entity.CreatePerson = model.CreatePerson;
                entity.CreateTime = model.CreateTime;
                entity.IsLast = model.IsLast;
                if (m_Rep.Create(entity))
                {
                    //分配给角色
                    m_Rep.P_Sys_InsertSysRight();
                    //清理无用的项
                    m_Rep.P_Sys_ClearUnusedRightOperate();
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }

          
        }
        public bool DeleteAndClear(ref ValidationErrors errors, string id)
        {
            try
            {
                //检查是否有下级
                if (m_Rep.GetChildrenCount(id)> 0)
                {
                    errors.Add("有下属关联，请先删除下属！");
                    return false;
                }
                
                if (m_Rep.Delete(id) > 0)
                {
                    //清理无用的项
                    m_Rep.P_Sys_ClearUnusedRightOperate();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }
     }
}
